/*
 * btvpms: com.ctvit.framework.excel.ExportService.java Create On Jun 15, 2009 5:11:37 PM By Q-Wang
 * $Revision: 1.2 $
 */
package com.ctvit.framework.service;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//import com.ctvit.framework.attachment.domain.SystemAttachment;
//import com.ctvit.framework.attachment.service.FileService;
import com.ctvit.framework.excel.ExcelExportListener;
import com.ctvit.framework.excel.ExcelTemplate;
import com.ctvit.framework.excel.PrintContext;
import com.ctvit.framework.web.context.ContextHolder;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

/**
 * excel文件导出服务.
 *
 * @author Q-Wang
 */
public class ExportService extends BaseService {
	
	private static final DateFormat pathFormat = new SimpleDateFormat("yyyy/MM/dd/");
	
//	private FileService fileService;
	
	/**
	 * @param fileService the fileService to set
	 */
//	public void setFileService(FileService fileService) {
//		this.fileService = fileService;
//	}
	//返回路径: /export/generated/yyyy/MM/dd/filename
	public String doExcelExport(String template, String filename, PrintContext context) {	
		 return doExcelExport(template,filename,context,null);
	}
	public String doWordExport(String filename,List jmdList){
		if (jmdList==null||jmdList.size()==0) throw new RuntimeException("读取节目单数据出错！");
		String path = "export/generated/" + pathFormat.format(new Date());
		File dir = new File(ContextHolder.getWebRoot() + path);
		dir.mkdirs();
//		String strTemplateFile = ContextHolder.getWebRoot() + "/export/template/" + template;
		String strDestFile = path+filename;
		try{
//			FileInputStream templateFile= new FileInputStream(strTemplateFile);
//			FileOutputStream destFile=new FileOutputStream(strDestFile);
			OutputStream os = new FileOutputStream(new File(dir,filename));
//			templateFile.read();
//			int len = 0;
//			byte[] buff=new byte[2048];
//			while ((len = (templateFile.read(buff, 0, 2048)))!=-1){
//				destFile.write(buff, 0, len);
//			}
//			templateFile.close();
			
			Document document = new Document(PageSize.A4);
			document.setMargins(3.17f*30,3.17f*30,2.54f*30,2.54f*30);
			RtfWriter2.getInstance(document, os);
			document.open();
			 // 设置中文字体        
			BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
			List curJmd=null;
			for(int i=0;i<jmdList.size();i++){
				curJmd=(List)jmdList.get(i);				
				// 标题字体风格        
				Font titleFont = new Font(bfChinese, 14, Font.BOLD);
				titleFont.setColor(Color.BLUE);
//				String titlestr=getJmdTitle(curJmd.get(0));
//				Paragraph title = new Paragraph(titlestr); 
//				// 设置标题格式对齐方式      
//				title.setAlignment(Element.ALIGN_LEFT);   
//				title.setFont(titleFont);       
//				document.add(title);
//				// 正文字体风格        
//				Font contextFont = new Font(bfChinese, 10, Font.NORMAL);
//				Paragraph context = new Paragraph(getJmdStr(curJmd));
//				context.setFont(contextFont);    
//				context.setLeading(16);
//				document.add(context);
//				if (i+1<jmdList.size()){
//					document.newPage();
//				}
			}
			document.close();
			os.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return strDestFile;
	}
	public String doExcelExport(String template, String filename, PrintContext context,ExcelExportListener listener) {
		// 路径: /export/generated/yyyy/MM/dd/filename
		String path = "export/generated/" + pathFormat.format(new Date());
		File dir = new File(ContextHolder.getWebRoot() + path);
		dir.mkdirs();
		try {
			OutputStream os = new FileOutputStream(new File(dir,filename));
			ExcelTemplate.output(template, context, os,listener);
//			SystemAttachment attachment = new SystemAttachment();
//			attachment.setFilename(filename);
//			attachment.setPath(path);
//			attachment.setUploader("Generated");
//			attachment.setUploadTime(new Date());
//			attachment.setLastModify(new Date());
//			return fileService.saveFile(attachment);
		} catch (FileNotFoundException e) {
			log.warn("文件写入失败:" + path + "/" + filename, e);
			throw new RuntimeException(e);
		} catch (Exception e) {
			log.warn("文件导出失败", e);
			throw new RuntimeException(e);
		}
		return path+"/"+filename;
	}
}
